package com.ukefu.util.ai;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.lionsoul.jcseg.extractor.impl.TextRankKeywordsExtractor;
import org.lionsoul.jcseg.tokenizer.core.ADictionary;
import org.lionsoul.jcseg.tokenizer.core.DictionaryFactory;
import org.lionsoul.jcseg.tokenizer.core.ISegment;
import org.lionsoul.jcseg.tokenizer.core.IWord;
import org.lionsoul.jcseg.tokenizer.core.JcsegException;
import org.lionsoul.jcseg.tokenizer.core.JcsegTaskConfig;
import org.lionsoul.jcseg.tokenizer.core.SegmentFactory;

public class DicSegment {
	
	//创建JcsegTaskConfig分词配置实例，自动查找加载jcseg.properties配置项来初始化
	private static JcsegTaskConfig config = new JcsegTaskConfig(true);

	//创建默认单例词库实现，并且按照config配置加载词库
	private static ADictionary dic = DictionaryFactory.createSingletonDictionary(config);
	
	private synchronized static ISegment getSegment() throws JcsegException{
		config.setAppendCJKSyn(false);
		//追加拼音, 需要在jcseg.properties中配置jcseg.loadpinyin=1
		config.setAppendCJKPinyin(false);
		ISegment seg = SegmentFactory.createJcseg(
				JcsegTaskConfig.COMPLEX_MODE, 
				new Object[]{config, dic}
		);
		return seg ;
	}

	//依据给定的ADictionary和JcsegTaskConfig来创建ISegment
	//为了Api往后兼容，建议使用SegmentFactory来创建ISegment对象
	//Segment 非线程安全，且切分速度非常快
	
	public synchronized static String[] segment(String content) throws IOException, JcsegException{
		IWord word = null;
		List<String> words = new ArrayList<String>();
		ISegment seg = getSegment();
		seg.reset(new StringReader(content));

		while ( (word = seg.next()) != null ) {
			if(word.getValue().length() == 1 && isChineseByBlock(word.getValue().charAt(0))){
				continue ;
			}else{
				words.add(word.getValue()) ;
			}
		}
		
		return words.toArray(new String[words.size()]);
	}
	
	public static String[] keyword(String content) throws IOException, JcsegException{
		return keyword(content , 20);
	}
	
	public static String[] keyword(String content , int num) throws IOException, JcsegException{
		ISegment seg = getSegment();
		TextRankKeywordsExtractor extractor = new TextRankKeywordsExtractor(seg);
		extractor.setMaxIterateNum(100);        //设置pagerank算法最大迭代次数，非必须，使用默认即可
		extractor.setWindowSize(5);             //设置textRank计算窗口大小，非必须，使用默认即可
		extractor.setKeywordsNum(10);           //设置最大返回的关键词个数，默认为10
		List<String> keywords = extractor.getKeywords(new StringReader(content));
		
		return keywords.toArray(new String[keywords.size()]);
	}
	
	public static boolean isChineseByBlock(char c) {
		Character.UnicodeScript sc = Character.UnicodeScript.of(c);	
		if (sc == Character.UnicodeScript.COMMON) {
            return true;
        } else {
            return false;
        }
    }
	
	public static void loadDic(String path) throws IOException{
		File dicPath = new File(path) ;
		if(!dicPath.exists()){
			dicPath.mkdirs() ;
		}
		dic.loadDirectory(path);
	}
	
	public static void removeWord(String word){
		int inx = ADictionary.getIndex(word) ;
		dic.remove(inx, word);
	}
}
